<? include ("../init.php") ?>
<? include ("../header.php") ?>
<? include ("../menu.php") ?>
<? include ("submenu.php") ?>
<?php

  $showcase_id = $_GET['aid'];

  $sql = "SELECT * FROM showcase WHERE showcase_id = '".$showcase_id."'";
  $line = $db->queryUniqueObject($sql);
  
  $sql = 'SELECT * FROM showcase_img WHERE showcase_id = '.$showcase_id.' ORDER BY img_position';
  $rss = $db->query($sql);
  $i = 1;
  $image_gallery = '';
  $lastPos = '';
  
  while ($line2 = $db->fetchNextObject($rss)){
    $image_gallery .= <<<image_gallery
      <li id="slideOrder_{$i}">
        <div>
          <img src="../images/ajax-loader.gif" class="ajax_load_img delete_indicator"/>
          <img src="../../images/showcase/{$showcase_id}/{$line2->showcase_img_path}" class="imgGallThumb" />
          <p>
            <a href="javascript:void(0);" id="{$line2->showcase_img_id}" class="aLink deleteImg">Delete</a>
          </p>
        </div>
        <span class="hiddenEle" id="imgOderNew"><p>{$line2->showcase_img_id}</p></span>
      </li>
image_gallery;
    ++$i;
    $lastPos = $line2->img_position;
  }
?>
<div id = "body">

<h1><?_l('edit')?> <?_l('showcase')?></h1>

<a href = "#" onclick = "uploadShowcaseIimg(<?php echo $showcase_id; ?>)" class = "add"><img src = "<?=ADMIN?>/images/icon-add.png">&nbsp;&nbsp;&nbsp;<?_l('upload-more-images')?></a>

<div class = "clear"></div>

<form method="post" action="update.php" class = "form" enctype="multipart/form-data">
  <input name="showcase_id" type="hidden" value="<?=$showcase_id?>"/>
  <input name="old_img" type="hidden" value="<?=$line->showcase_img?>" />
  <div class = "field"><label><?_l('location')?></label><input type = "text" name = "location" class = "input size1" value = "<?=$line->showcase_place?>" ></div>
  <div class = "field"><label><?_l('image')?></label><input type = "file" name = "showcase_img" class = "input size1" ></div>
  <?php
    if($line->showcase_img!='' && file_exists('../../images/showcase/'.$showcase_id.'/'.$line->showcase_img)){
      echo '<div class = "field"><label>&nbsp;</label><img src="../../images/showcase/'.$showcase_id.'/'.$line->showcase_img.'" /></div>';
    }
  ?>
  <div class = "field"><label><?_l('detail')?></label><input type = "text" name = "detail" class = "input size6" value = "<?=$line->showcase_detail?>" ></div>
  
  <div class = "field">
    <label><?_l('gallery')?></label>
    ลากรูปภาพเพื่อจัดตำแหน่ง จากนั้นกดปุ่มอัพเดต
  </div>
  
  <div class = "field">
    <div id="tabs-1" class="imagesOrder">
      <!--<form name="imagesOrderFrm" id="imagesOrderFrm">-->
        <ul class="imagesOrderlist" id="list1">
        <?php echo $image_gallery; ?>
        </ul>
        <div class = "clear"></div>
      <!--</form>-->
      <input name="list1SortOrder" type="hidden" />
    </div>
  </div>
  <br />
  <div class = "field"><label></label>
    <input type="submit" name="save" value = "<?_l('update')?>" class = "btn" />
    <input onclick = "javascript:history.back(1)" type = "button" value="<?_l('cancel')?>" class = "btn" />
  </div>
  
  <div class = "clear"></div>
</form>

</div>


<style>
  .form label {
    width:80px;
  }
</style>
<script type="text/javascript" src="../js/jquery.dragsort-0.3.10.js"></script>
    <script type="text/javascript">
	$(document).ready(function(){
	    $('.deleteImg').click(function(){
		
		var imgEle = $(this);
		var id = imgEle.attr('id');
		
		if(confirm('Sure?'))
		{
		    imgEle.parents("li").find('.delete_indicator').css('display','inline');
		    $.post('del.php','imgId='+id,function(data){
			imgEle.parents("li").remove();
		    });
		}
	    });
	    
	    $("#list1").dragsort({ dragSelector: "div", dragBetween: true, dragEnd: saveOrder, placeHolderTemplate: "<li class='placeHolder'><div></div></li>" });
	});
	
	function saveOrder() {
	    var serialStr = "";
	    $("#list1 li span").each(function(i, elm) {
		serialStr += (i > 0 ? "|" : "") + $(elm).children("p").html();
	    });
	    
	    $("input[name=list1SortOrder]").val(serialStr);
	};
    </script>
<? include ("../footer.php") ?>